package com.meiqiongproperty.yiguanjia.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.meiqiongproperty.yiguanjia.db.MyDB;
import com.meiqiongproperty.yiguanjia.db.table.DefaultHouseTable;
import com.meiqiongproperty.yiguanjia.httpbean.UserInfo;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;

import java.util.ArrayList;
import java.util.Locale;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.db.dao
 * @User ChenJing
 * @Date 2017/5/22
 */

public class DefaultHouseDao implements DefaultHouseTable {
    private static final String TAG = DefaultHouseTable.class.getSimpleName();

    private SQLiteDatabase mDb;

    /**
     * 将上层的context传到Dao层
     *
     * @param context
     */
    public DefaultHouseDao(Context context) {
        mDb = MyDB.getInstance(context).getDB();
        if (mDb == null) {
            Log.e(TAG, "init SQLiteDatabase failed ");
        }
    }

    /**
     * 添加一条
     *
     * @param defaultHouseBean
     * @return
     */
    public long add(UserInfo.DefaultHouseBean defaultHouseBean) {
        // 入库成功后的id。
        long id = -1;
        if (null == defaultHouseBean) {
            Log.w(TAG, "Add note:note is null!");
            return id;
        }
        // 入库数值。
        ContentValues values = new ContentValues();
        values.put(ID,defaultHouseBean.getID());
        values.put(COMMUNITYID, defaultHouseBean.getCommunityId());
        values.put(PROVINCE, defaultHouseBean.getProvince());
        values.put(CITY, defaultHouseBean.getCity());
        values.put(AREA, defaultHouseBean.getArea());
        values.put(COMMUNITYNAME, defaultHouseBean.getCommunityName());
        values.put(BUILD, defaultHouseBean.getBuild());
        values.put(ROOM,defaultHouseBean.getRoom());
        values.put(ROOMAREA, defaultHouseBean.getRoomArea());
        values.put(ROOMPRICE, defaultHouseBean.getRoomPrice());
        values.put(COMMUNITYADDRESS, defaultHouseBean.getCommunityAddress());
        values.put(BUTLERID, defaultHouseBean.getButlerId());
        values.put(BUTLERNAME, defaultHouseBean.getButlerName());
        values.put(BUTLERPHONE, defaultHouseBean.getButlerPhone());
        values.put(PROPERTYNAME, defaultHouseBean.getPropertyName());
        values.put(PROPERTYID, defaultHouseBean.getPropertyId());
        values.put(PROPERTYPHONE, defaultHouseBean.getPropertyPhone());
        values.put(DEFAULTS, defaultHouseBean.getDefault());
        values.put(AUDIT, defaultHouseBean.getAudit());
        LogUtil.d("mDb：" + mDb);
        // 入库。
        id = mDb.insert(TABLE_NAME, null, values);
        return id;
    }

    /**
     * 通过ID更新
     *
     * @param id
     * @param defaultHouseBean
     * @return
     */
    public int update(int id, UserInfo.DefaultHouseBean defaultHouseBean) {
        int updateCount = 0;
        // 参数检查
        if (id < 0 || (defaultHouseBean == null)) {
            Log.w(TAG, "update note,id = " + id);
            return updateCount;
        }
        // 匹配条件
        String whereClause = String.format("%s=?", DefaultHouseTable.ID);
        String[] whereArgs = new String[]{String.valueOf(id)};
        // 入库值
        ContentValues values = new ContentValues();
        values.put(ID,defaultHouseBean.getID());
        values.put(COMMUNITYID, defaultHouseBean.getCommunityId());
        values.put(PROVINCE, defaultHouseBean.getProvince());
        values.put(CITY, defaultHouseBean.getCity());
        values.put(AREA, defaultHouseBean.getArea());
        values.put(COMMUNITYNAME, defaultHouseBean.getCommunityName());
        values.put(BUILD, defaultHouseBean.getBuild());
        values.put(ROOM,defaultHouseBean.getRoom());
        values.put(ROOMAREA, defaultHouseBean.getRoomArea());
        values.put(ROOMPRICE, defaultHouseBean.getRoomPrice());
        values.put(COMMUNITYADDRESS, defaultHouseBean.getCommunityAddress());
        values.put(BUTLERID, defaultHouseBean.getButlerId());
        values.put(BUTLERNAME, defaultHouseBean.getButlerName());
        values.put(BUTLERPHONE, defaultHouseBean.getButlerPhone());
        values.put(PROPERTYNAME, defaultHouseBean.getPropertyName());
        values.put(PROPERTYID, defaultHouseBean.getPropertyId());
        values.put(PROPERTYPHONE, defaultHouseBean.getPropertyPhone());
        values.put(DEFAULTS, defaultHouseBean.getDefault());
        values.put(AUDIT, defaultHouseBean.getAudit());
        try {
            updateCount = mDb.update(DefaultHouseTable.TABLE_NAME, values, whereClause,
                    whereArgs);
        } catch (Exception e) {
            Log.e(TAG, "update note table error! count = " + updateCount);
        }
        return updateCount;
    }


    /**
     * 通过ID删除
     *
     * @param user_id
     * @return
     */
    public int delete(String user_id) {
        // 创建返回值
        int delCount = 0;
        // 参数检查
//        if (id < 0) {
//            Log.w(TAG, "query note,id = " + id);
//            return delCount;
//        }
        // 匹配条件
        String whereClause = String.format("%s=?", ID);
        String[] whereArgs = new String[]{user_id};
        try {
            delCount = mDb.delete(DefaultHouseTable.TABLE_NAME, whereClause, whereArgs);
        } catch (Exception e) {
            Log.e(TAG, "delete note table error! id =" + delCount);
        }
        return delCount;
    }

    /**
     * 删除全部
     *
     * @return
     */
    public int deleteAll() {
        int delCount = 0;
        try {
            delCount = mDb.delete(DefaultHouseTable.TABLE_NAME, null, null);

        } catch (Exception e) {
            Log.e(TAG, "delete note table error! id =" + delCount);
        }
        return delCount;
    }

    /**
     * 通过ID查询
     *
     * @param id
     * @return
     */
    public UserInfo.DefaultHouseBean query(int id) {
        // 参数检查
        if (id < 0) {
            Log.w(TAG, "query note,id = " + id);
            return null;
        }
        // 创建返回值
        UserInfo.DefaultHouseBean defaultHouseBean = null;
        // 查询条件
        String selection = String.format(Locale.US, "%s='%s'", ID, id);
        // 获取游标
        Cursor cursor = null;
        try {
            cursor = mDb.query(TABLE_NAME, null, selection, null, null, null,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                // 将数值设置到实体类中
                defaultHouseBean = new UserInfo.DefaultHouseBean();
                defaultHouseBean.setID(cursor.getString(INDEX_ID));
                defaultHouseBean.setCommunityId(cursor.getString(INDEX_COMMUNITYID));
                defaultHouseBean.setProvince(cursor.getString(INDEX_PROVINCE));
                defaultHouseBean.setCity(cursor.getString(INDEX_CITY));
                defaultHouseBean.setArea(cursor.getString(INDEX_AREA));
                defaultHouseBean.setCommunityName(cursor.getString(INDEX_COMMUNITYNAME));
                defaultHouseBean.setBuild(cursor.getString(INDEX_BUILD));
                defaultHouseBean.setBuild(cursor.getString(INDEX_BUILD));
                defaultHouseBean.setRoom(cursor.getString(INDEX_ROOM));
                defaultHouseBean.setRoomArea(cursor.getString(INDEX_ROOMAREA));
                defaultHouseBean.setRoomPrice(cursor.getString(INDEX_ROOMPRICE));
                defaultHouseBean.setCommunityAddress(cursor.getString(INDEX_COMMUNITYADDRESS));
                defaultHouseBean.setButlerId(cursor.getString(INDEX_BUTLERID));
                defaultHouseBean.setButlerName(cursor.getString(INDEX_BUTLERNAME));
                defaultHouseBean.setPropertyPhone(cursor.getString(INDEX_PROPERTYPHONE));
                defaultHouseBean.setButlerPhone(cursor.getString(INDEX_BUTLERPHONE));
                defaultHouseBean.setPropertyName(cursor.getString(INDEX_PROPERTYNAME));
                defaultHouseBean.setPropertyId(cursor.getString(INDEX_PROPERTYID));
                defaultHouseBean.setDefault(cursor.getString(INDEX_DEFAULT));
                defaultHouseBean.setAudit(cursor.getString(INDEX_AUDIT));
            }
        } catch (SQLiteException e) {
            Log.e(TAG, "---Query failed!" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return defaultHouseBean;
    }

    /**
     * 查询全部
     *
     * @return
     */
    public ArrayList< UserInfo.DefaultHouseBean> queryAll() {
        // 创建返回值
        ArrayList< UserInfo.DefaultHouseBean> defaultHouseBeanArrayList = new ArrayList<>();
        // 查询条件
        String order = ID + " DESC";
        // 获取游标
        Cursor cursor = null;
        UserInfo.DefaultHouseBean defaultHouseBean = null;
        try {
            cursor = mDb.query(TABLE_NAME, null, null, null, null, null, order);
            if (cursor != null && cursor.moveToFirst()) {
                // 循环取出所有数值
                do {
                    // 赋值
                    defaultHouseBean = new UserInfo.DefaultHouseBean();
                    defaultHouseBean.setID(cursor.getString(INDEX_ID));
                    defaultHouseBean.setCommunityId(cursor.getString(INDEX_COMMUNITYID));
                    defaultHouseBean.setProvince(cursor.getString(INDEX_PROVINCE));
                    defaultHouseBean.setCity(cursor.getString(INDEX_CITY));
                    defaultHouseBean.setArea(cursor.getString(INDEX_AREA));
                    defaultHouseBean.setCommunityName(cursor.getString(INDEX_COMMUNITYNAME));
                    defaultHouseBean.setBuild(cursor.getString(INDEX_BUILD));
                    defaultHouseBean.setBuild(cursor.getString(INDEX_BUILD));
                    defaultHouseBean.setRoom(cursor.getString(INDEX_ROOM));
                    defaultHouseBean.setRoomArea(cursor.getString(INDEX_ROOMAREA));
                    defaultHouseBean.setRoomPrice(cursor.getString(INDEX_ROOMPRICE));
                    defaultHouseBean.setCommunityAddress(cursor.getString(INDEX_COMMUNITYADDRESS));
                    defaultHouseBean.setButlerId(cursor.getString(INDEX_BUTLERID));
                    defaultHouseBean.setButlerName(cursor.getString(INDEX_BUTLERNAME));
                    defaultHouseBean.setPropertyPhone(cursor.getString(INDEX_PROPERTYPHONE));
                    defaultHouseBean.setButlerPhone(cursor.getString(INDEX_BUTLERPHONE));
                    defaultHouseBean.setPropertyName(cursor.getString(INDEX_PROPERTYNAME));
                    defaultHouseBean.setPropertyId(cursor.getString(INDEX_PROPERTYID));
                    defaultHouseBean.setDefault(cursor.getString(INDEX_DEFAULT));
                    defaultHouseBean.setAudit(cursor.getString(INDEX_AUDIT));
                    // 添加到集合中
                    defaultHouseBeanArrayList.add(defaultHouseBean);
                } while (cursor.moveToNext());
            }
        } catch (SQLiteException e) {
            Log.e(TAG, "Query all note failed!" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return defaultHouseBeanArrayList;
    }
}